package org.example;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;

public class Main {
    public static void main(String[] args) throws InterruptedException {
        //test1();
        //test2();
        //test3();
        //test4();
        //test5();
        //test6();
        //test7();
        //test8();
        //test9();
        test10();
    }

    private static void test10() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
        sleep(3000);
        // 通过getWindowHandles获取所有的窗口句柄
        // 通过getWindowHandle获取的get打开的页面窗口句柄
        System.out.println(webDriver.getWindowHandle());
        Set<String> handles = webDriver.getWindowHandles();
        String target_handle = "";
        for(String handle:handles) {
            target_handle = handle;
        }
        webDriver.switchTo().window(target_handle);
        sleep(3000);
        webDriver.close();
//        webDriver.findElement(By.cssSelector("#ww")).sendKeys("新闻联播");
//        webDriver.findElement(By.cssSelector("#s_btn_wr")).click();
    }
    private static void test9() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("520");
        webDriver.findElement(By.cssSelector("#su")).click();
        //找到图片
        sleep(2000);
        WebElement webElement = webDriver.findElement(By.cssSelector("#\\31  > div > h3 > a > div > div > p > span > span"));
        sleep(2000);
        //鼠标对象
        Actions actions = new Actions(webDriver);
        //鼠标移到图片
        actions.moveToElement(webElement);
        //右击
        actions.contextClick().perform();
    }

    private static void test8() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        //搜索520
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("520");
        //Ctrl + A
        sleep(2000);
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL, "A");
        //Ctrl + X
        sleep(2000);
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL, "X");
        //Ctrl + V
        sleep(2000);
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL, "V");
        sleep(2000);
        webDriver.quit();
    }

    private static void test7() throws InterruptedException {
        //浏览器页面操作  前进回退
        //滑动条
        //浏览器大小缩放
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        //浏览器输入520，点击
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("520");
        webDriver.findElement(By.cssSelector("#su")).click();
        //等待2秒，回退
        sleep(2000);
        webDriver.navigate().back();
        //等待2秒，刷新
        sleep(2000);
        webDriver.navigate().refresh();
        //等待2秒，前进
        sleep(2000);
        webDriver.navigate().forward();
        //等待两秒，滑动到指定位置
        sleep(2000);
        //需要使用JS
        ((JavascriptExecutor)webDriver).executeScript("document.documentElement.scrollTop=10000");
        //等待两秒，浏览器放大到最大
        sleep(2000);
        webDriver.manage().window().maximize();
        //充满屏幕
        sleep(2000);
        webDriver.manage().window().fullscreen();
        //自定义大小
        sleep(2000);
        webDriver.manage().window().setSize(new Dimension(700, 900));
        sleep(2000);
        webDriver.quit();
    }

    private static void test6() {
        //隐式等待和显示等待
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("520");
        webDriver.findElement(By.cssSelector("#su")).click();
        //隐式等待1天
        //webDriver.manage().timeouts().implicitlyWait(1, TimeUnit.DAYS);
        //显示等待
        WebDriverWait wait = new WebDriverWait(webDriver, 10);
        //显示等待标题是否为指定内容
        //wait.until(ExpectedConditions.titleIs("520_百度搜索"));
        //显示等待该元素可见
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#\\31  > div > div._content-border_zc167_4.content-border_r0TOp.cu-border.sc-aladdin.sc-cover-card > div > div:nth-child(2) > div > div > div:nth-child(1) > div > div > div > div > div > div._info-mask_bo7t2_25")));
    }

    public static void test5() {
        //获取当前URL和Title
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        String url = webDriver.getCurrentUrl();
        String title = webDriver.getTitle();
        if(url.equals("https://www.baidu.com/") && title.equals("百度一下，你就知道")){
            System.out.println("测试通过");
        }
        else {
            System.out.println("测试不通过");
            System.out.println("url: " + url);
            System.out.println("title: " + title);
        }
    }
    public static void test4() {
        //非form内标签使用submit会报错
        //form内标签使用submit相当于click
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
//        //定位搜索栏  form标签内
//        WebElement element = webDriver.findElement(By.cssSelector("#kw"));
//        //输入内容
//        element.sendKeys("Java");
//        //提交，效果类似点击按钮
//        element.submit();

        //定位“新闻”  form标签外
        WebElement element = webDriver.findElement(By.xpath("//a[text()=\"新闻\"]"));
        element.submit();//不行
        //element.click();//可以
    }
    public static void test3() throws InterruptedException {
        //getAttribute可以获取属性值
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        //"百度一下"按钮
        WebElement element = webDriver.findElement(By.cssSelector("#su"));
        String text = element.getAttribute("value");
        if(text.equals("百度一下")){
            System.out.println("测试通过");
        }
        else{
            System.out.println("测试不通过, 获取到的内容为: " + text);
        }
    }
    public static void test2() throws InterruptedException {
        //使用clear
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        //定位搜素框
        WebElement element = webDriver.findElement(By.cssSelector("#kw"));//id选择器
        //输入内容
        element.sendKeys("Selenium");
        //定位“百度一下”按钮，并点击
        webDriver.findElement(By.cssSelector("#su")).click();
        //等待资源加载完
        sleep(2000);
        //清空输入栏内容
        webDriver.findElement(By.cssSelector("#kw")).clear();
    }
    private static void test1() throws InterruptedException {
        //直接使用驱动
        WebDriver webDriver = new ChromeDriver();
        //通过驱动访问指定URL
        webDriver.get("https://www.baidu.com");
        //定位元素——输入框
        WebElement search = webDriver.findElement(By.cssSelector(".s_ipt"));//CSS——类选择器
        search.sendKeys("软件测试");//输入搜索
        //定位元素——“百度一下”按钮
        WebElement button = webDriver.findElement(By.xpath("//form/span[2]/input"));
        button.click();//点击按钮
        sleep(2000);//等待搜索结果
        //检测结果
        List<WebElement> elements= webDriver.findElements(By.cssSelector("a em"));
        int flag = 0;
        for(int i = 0; i < elements.size(); ++i)
        {
            if(elements.get(i).getText().equals("测试")) {
                flag = 1;
                break;
            }
        }
        if(flag == 1)
            System.out.println("测试通过");
        else
            System.out.println("测试不通过");
    }
}